package com.ita.shoppingcart.cart;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ita.shoppingcart.bean.Goods;
import com.ita.shoppingcart.dao.GoodsDao;
import com.ita.shoppingcart.daoImpl.GoodsDaoImpl;
import com.ita.shoppingcart.daoImpl.GoodsDaoImpl_;

public class CartManagerImpl implements CartManager {
	
//	private Map<Integer, Goods> cart = new HashMap<Integer, Goods>();
	private GoodsDao goodsDaoImpl = new GoodsDaoImpl_();

	@Override
	public Map<Integer, Goods> pushToCart(int id, Map<Integer, Goods> cart) {
		// TODO Auto-generated method stub
		int befortSize = cart.size();
		if(cart.containsKey(id)){
			cart.get(id).setCount(cart.get(id).getCount() + 1);
		}else {			
			Goods goods = goodsDaoImpl.findGoods(id);
			goods.setCount(1);
			cart.put(id, goods);
		}
		return cart;
	}

	@Override
	public List<Goods> showCart(Map<Integer, Goods> cart) {
		// TODO Auto-generated method stub
		List<Goods> list = new ArrayList<Goods>();		
		for (Integer key : cart.keySet()) {
			list.add(cart.get(key));
		}
		return list;
	}

	@Override
	public Map<Integer, Goods> removeFromCart(int id, Map<Integer, Goods> cart) {
		// TODO Auto-generated method stub
		if(cart.containsKey(id)){
			cart.remove(id);
		}
		return cart;
	}

	@Override
	public int goodsCount(Map<Integer, Goods> cart) {
		// TODO Auto-generated method stub
		return cart.size();
	}

	@Override
	public double goodsTotal(Map<Integer, Goods> cart) {
		// TODO Auto-generated method stub
		double total = 0;
		for (Integer key : cart.keySet()) {
			total = total + (cart.get(key).getPrice() * cart.get(key).getCount());
		}
		return total;
	}

}
